Imports System.IO

Partial Public Class editForm

    'Protected _stationsList As List(Of OnlineStation)
    Protected _backupList As StationList
    Protected _radio As Radio
    Protected _isDirty As Boolean

    Private Sub New()
        InitializeComponent()
        Init(Nothing)
    End Sub

    Sub New(ByVal radio As Radio)
        InitializeComponent()
        Init(radio)
    End Sub

    'initializes the form
    Sub Init(ByVal radio As Radio)

        _radio = radio
        _isDirty = False

        'save a backup of OnlineStation instances
        'WARN:Exact reference to supplied OnlineStation instances should be maintained
        'instead of copying or serializing so if a "rollback" occurs we can offer the
        'exact supplied instaces back untinkered.
        _backupList = New StationList()
        _backupList.Stations.AddRange(_radio.Stations)

        ResetBindings()

    End Sub

    'binds or rebinds the data gridview data source
    Shadows Sub ResetBindings()
        stationsBinding.DataSource = _radio.Stations
    End Sub

    'restore stations list from backup memory
    Sub RestoreBackup()

        stationsBinding.SuspendBinding()

        'loads from the backup list
        _radio.Stations.Clear()
        _radio.Stations.AddRange(_backupList.Stations)

        'resume and refresh bindings
        ResetBindings()
        stationsBinding.ResumeBinding()

        'clear the dirty flag
        Me.IsDirty = False

    End Sub

    Public Property IsDirty() As Boolean
        Get
            Return _isDirty
        End Get
        Protected Set(ByVal value As Boolean)
            _isDirty = value
        End Set
    End Property

    Private Sub closeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeButton.Click

        'commit any current edits
        stationsGrid.CommitEdit(DataGridViewDataErrorContexts.InitialValueRestoration)

        'and then close the form
        Me.Close()

    End Sub

    Private Sub newButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newButton.Click

        'add a new station to the data source
        Dim insertedRowIdx = stationsBinding.Add(New OnlineStation())

        'lets the user begin editing the new row immediatly
        stationsGrid.ClearSelection()
        stationsGrid.CurrentCell = stationsGrid.Rows(insertedRowIdx).Cells(0)
        stationsGrid.BeginEdit(True)

        'and flag as dirty
        Me.IsDirty = True

    End Sub

    Private Sub removeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeButton.Click

        'remove currently selected item
        stationsBinding.RemoveCurrent()
        Me.IsDirty = True

    End Sub

    Private Sub stationsGrid_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles stationsGrid.CellEndEdit
        Me.IsDirty = True
    End Sub

    Private Sub importButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles importButton.Click

        'shows import file dialog
        If ofdImport.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Return

        'opens from selected file
        'added stations from selected file to current list (append:=True)
        'instead of replacing it entirely
        _radio.LoadStationList(ofdImport.FileName, append:=True)

        'refresh data bindings
        stationsBinding.ResetBindings(metadataChanged:=False)
        Me.IsDirty = True

    End Sub

    Private Sub exportButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exportButton.Click

        'shows export file dialog
        If sfdExport.ShowDialog = Windows.Forms.DialogResult.Cancel Then Return

        'saves to selected file
        _radio.SaveStationList(sfdExport.FileName)

    End Sub

    Private Sub discardChangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles discardChangeButton.Click

        'restore from backup and close the form
        Me.RestoreBackup()
        Me.Close()

    End Sub

    Private Sub clearStationsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearStationsButton.Click

        'clear the data source
        stationsBinding.Clear()

    End Sub

End Class